package com.century.sjt.window;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.century.sjt.R;
import com.century.sjt.activity.AddBankActivity;
import com.century.sjt.activity.Pay.MerchantActivity;
import com.century.sjt.entity.BankCard;

import java.util.ArrayList;
import java.util.List;

/**
 * @author:周玲波
 * @Description: ${todo}(用一句话描述该文类做什么)
 * @date : 2016/8/20 15:54
 */
public class PayPopupWindowS extends PopupWindow {
    private Context mContext;
    ChoosePayPopupWindowS menuWindow;
    private Button btn_ok;
    private View mMenuView;
    private RequestQueue mQueue;
    //翻译监听
    private PayWindowListItemClickListener  listener;
    private BankCard payBankCard;
    private List<BankCard> mCardList = new ArrayList<>();
    private List<BankCard> mMTList = new ArrayList<>();
    private LinearLayout layout_Balance,layout_Quick_pay,layout_Phone_pay,layout_Addbankpay,layout_Card_pay;
    private TextView tv_Balance;
    private String amount;
    private String Type;
    private String Balance;

    public CheckBox checkBox;

    private View view_1,view_2;

    public String getAmount() {
        return amount;
    }

    public void setAmount(String amount) {
        this.amount = amount;
    }

    public List<BankCard> getmCardList() {
        return mCardList;
    }

    public void setmCardList(List<BankCard> mCardList) {
        this.mCardList = mCardList;
    }

    public List<BankCard> getmMTList() {
        return mMTList;
    }

    public void setmMTList(List<BankCard> mMTList) {
        this.mMTList = mMTList;
    }

    public String getBalance() {
        return Balance;
    }

    public void setBalance(String balance) {
        Balance = balance;
        tv_Balance.setText(balance);
    }

    public PayPopupWindowS(final Activity context, final String Type) {
        super(context);
        this.mContext = context;
        this.Type = Type;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.sjt_pay_window_s, null);
        mQueue = Volley.newRequestQueue(mMenuView.getContext());
        btn_ok = (Button) mMenuView.findViewById(R.id.btn_payok);
        checkBox = (CheckBox) mMenuView.findViewById(R.id.item_check);
        checkBox.setChecked(true);
        layout_Balance = (LinearLayout) mMenuView.findViewById(R.id.layout_pay1);
        layout_Quick_pay = (LinearLayout) mMenuView.findViewById(R.id.layout_pay2);
        layout_Phone_pay = (LinearLayout) mMenuView.findViewById(R.id.layout_pay3);
        layout_Card_pay = (LinearLayout) mMenuView.findViewById(R.id.layout_pay4);
        layout_Addbankpay = (LinearLayout) mMenuView.findViewById(R.id.layout_addbankpay);
        view_1 = (View) mMenuView.findViewById(R.id.view_1);
        view_2 = (View) mMenuView.findViewById(R.id.view_2);
        //设置按钮监听

        //设置PayPopupWindow的View
        this.setContentView(mMenuView);
        //设置PayPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
        //设置PayPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置PayPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置PayPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000);
        //设置PayPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        if ("1".equals(Type)){
            layout_Balance.setVisibility(View.GONE);
            layout_Card_pay.setVisibility(View.GONE);
            view_2.setVisibility(View.GONE);
            view_1.setVisibility(View.GONE);
        }
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                int y=(int) event.getY();
                if(event.getAction()==MotionEvent.ACTION_UP){
                    if(y<height){
                        dismiss();
                    }
                }
                return true;
            }
        });
//        menuWindow = new ChoosePayPopupWindow(context,getmCardList(),"0");
        layout_Balance.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                if (checkBox.isChecked()){
//                    checkBox.setChecked(false);
//                }else {
//                    checkBox.setChecked(true);
//                }
                listener.onPayTypeClick("0");
                payBankCard = new BankCard();
                payBankCard.setCardName(mContext.getResources().getString(R.string.text_blance_pay));
                payBankCard.setBankCardNo(getBalance());
                listener.onItemClick(payBankCard);
                dismiss();
            }
        });
        layout_Quick_pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShowChoseWindow(context,getmCardList(),"1");
//                listener.onPayTypeClick("1");
            }
        });
        layout_Phone_pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShowChoseWindow(context,getmMTList(),"2");
//                listener.onPayTypeClick("2");
            }
        });
        layout_Card_pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onPayTypeClick("3");
                payBankCard = new BankCard();
                payBankCard.setCardName(mContext.getResources().getString(R.string.text_bankcard_pay));
                payBankCard.setBankCardNo(getBalance());
                listener.onItemClick(payBankCard);
                dismiss();
            }
        });
        layout_Addbankpay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                Intent intent = new Intent();
                intent.setClass(mContext, AddBankActivity.class);
                mContext.startActivity(intent);

            }
        });
        tv_Balance = (TextView) mMenuView.findViewById(R.id.tv_balance);
        //确认按钮
        btn_ok.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                //销毁弹出框
                dismiss();
            }
        });

    }
    public void ShowChoseWindow(Activity context, List<BankCard> mlist,String type){
        menuWindow = new ChoosePayPopupWindowS(context,mlist,type);
//        menuWindow.notifyDataSetChanged();
        menuWindow.showAtLocation(context.findViewById(R.id.paymacth), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
        menuWindow.setChoosePayWindowListItemClickListener(new ChoosePayPopupWindowS.PayChooseWindowListItemClickListener() {
            @Override
            public void onItemClick(BankCard message) {
                listener.onItemClick(message);
                dismiss();
            }

            @Override
            public void onPayTypeClick(String Type) {
                listener.onPayTypeClick(Type);
            }
        });
    }

    public void showAppayRes(final String res) {
        new AlertDialog.Builder(mContext)
                .setMessage(res)
                .setTitle("提示")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        Intent intent = new Intent();
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        intent.putExtra("payType", "2");
                        intent.setClass(mContext, MerchantActivity.class);
                        mContext.startActivity(intent);

                    }
                })
                .show();
    }
    public void setPayWindowListItemClickListener(PayWindowListItemClickListener listener) {
        this.listener = listener;
    }
    public interface PayWindowListItemClickListener{
        /**
         * 支付方式事件监听
         * @param message
         * @return
         */
        void onItemClick(BankCard message);

        void onPayTypeClick(String Type);

    }




}
